setwd("/Users/justingeorge/Library/CloudStorage/OneDrive-TexasA&MUniversity/Research/Defence Economics/JES_2025/Replication-materials")
df<-read.csv("figures_data.csv")

library(tidyverse)

usa_data <- df %>%
  filter(ccode == 2) %>%  # Filter only rows where ccode == 2
  group_by(year) %>%
  summarize(share_usa = first(share), .groups = "drop")  # Ensure only one value per year

others <- df %>%
  filter(ccode != 2) %>%  # Exclude rows where ccode is 2 (USA)
  group_by(year) %>%
  summarize(share_others = mean(share, na.rm = TRUE), .groups = "drop")  # Calculate mean of `share`


graph1 <- others %>%
  inner_join(usa_data, by = "year")

graph1 <- graph1 %>%
  mutate(us_share = share_usa*100,
         other_share = share_others*100)
library(ggplot2)

ggplot(graph1, aes(x = year)) +
  # Plot line for US share
  geom_line(aes(y = us_share, color = "US"), size = 1.2) +
  # Plot line for non-US NATO allies
  geom_line(aes(y = other_share, color = "Non-US NATO Allies"), size = 1.2) +
  # Add solid vertical lines for significant years
  geom_vline(xintercept = c(1974, 1991, 2014, 2022), linetype = "dashed", color = "black", size = 0.8) +
  # Offset labels for vertical lines to avoid overlap
  annotate("text", x = 1974 + 1, y = 0.5 * max(c(graph1$us_share, graph1$other_share), na.rm = TRUE),
           label = "Flexible Response", angle = 90, hjust = -0.64, fontface = "bold", color = "black",size=4.5) +
  annotate("text", x = 1991 + 1, y = 0.5 * max(c(graph1$us_share, graph1$other_share), na.rm = TRUE),
           label = "End of Cold War", angle = 90, hjust = -0.70, fontface = "bold", color = "black",size=4.5) +
  annotate("text", x = 2014 + 1, y = 0.5 * max(c(graph1$us_share, graph1$other_share), na.rm = TRUE),
           label = "Crimean Invasion", angle = 90, hjust = -0.65, fontface = "bold", color = "black",size=4.5) +
  annotate("text", x = 2022 + 1, y = 0.5 * max(c(graph1$us_share, graph1$other_share), na.rm = TRUE),
           label = "Ukraine War", angle = 90, hjust = -1.0, fontface = "bold", color = "black",size=4.5) +
  # Labels and title
  labs(
    x = "Year",
    y = "ME as a percentage of GDP",
    color = "Group"
  ) +
  # Improved color scheme
  scale_color_manual(values = c("US" = "red", "Non-US NATO Allies" = "blue"),name = NULL,
                     limits = c("US", "Non-US NATO Allies"))+
  # Customize the x-axis with tick marks every 5 years
  scale_x_continuous(
    breaks = seq(min(graph1$year, na.rm = TRUE), max(graph1$year, na.rm = TRUE), by = 10),
    minor_breaks = NULL
  ) +
  # Customize theme
  theme_minimal(base_size = 14) +
  theme(
    axis.text = element_text(color = "black",size = 16),
    axis.title = element_text(color = "black", size = 16),
    axis.line = element_line(color = "black", size = 1), # Make axis lines bold and visible
    axis.ticks = element_line(color = "black", size = 1),  # Add bold tick marks
    axis.ticks.length = unit(0.25, "cm"),  # Increase tick mark length
    legend.position = "top",                  # Keep the legend at the top
    legend.text = element_text(size = 14),  # Increase legend title size
    panel.grid.major = element_blank(),  # Remove all grid lines
    panel.grid.minor = element_blank()   # Remove minor grid lines
  )

# Add white background to the plot
plot_with_white_bg <- last_plot() + 
  theme(
    plot.background = element_rect(fill = "white", color = NA),
    panel.background = element_rect(fill = "white", color = NA)
  )

# Save the plot
ggsave(
  filename = "figure_1.png", 
  plot = plot_with_white_bg, 
  width = 10, 
  height = 6, 
  dpi = 300, 
  units = "in"
)


usa_me <- df %>%
  filter(ccode == 2) %>%  # Filter only rows where ccode == 2
  group_by(year) %>%
  summarize(usa_me = first(me), .groups = "drop")  # Ensure only one value per year


others_me <- df %>%
  group_by(year) %>%
  summarize(nato_me = sum(me,na.rm = TRUE) )

graph2 <- others_me %>%
  inner_join(usa_me, by = "year")

graph2 <- graph2 %>%
  mutate(us_share = (usa_me/nato_me)*100)

ggplot(graph2, aes(x = year)) +
  # Plot line for 'us_share'
  geom_line(aes(y = us_share), color = "red", size = 1) +
  # Add solid vertical lines for specific years
  geom_vline(xintercept = c(1974, 1991, 2014, 2022), linetype = "dashed", color = "black") +
  # Add labels for the vertical lines
  annotate("text", x = 1974, y = 78.5, label = "Flexible Response", angle = 90, vjust = -0.6, hjust = -0.14,color = "black", fontface = "bold", size=4.5) +
  annotate("text", x = 1991, y = 78.5, label = "End of Cold War", angle = 90, vjust = -0.6, hjust = -0.14,color = "black", fontface = "bold",size=4.5) +
  annotate("text", x = 2014, y = 78.5, label = "Crimean Invasion", angle = 90, vjust = -0.6, hjust = -0.14, color = "black", fontface = "bold",size=4.5) +
  annotate("text", x = 2022, y = 78.5, label = "Ukraine War", angle = 90, vjust = -0.6, hjust = -0.34,color = "black", fontface = "bold",size=4.5)+
  # Add labels and title
  labs(
    x = "Year", 
    y = "US ME as a percentage of NATO ME", 
    color = "Variable"
  ) +
  # Customize the theme
  theme_minimal() +
  theme(
    axis.text = element_text(color = "black",size = 16),
    axis.title = element_text(color = "black",size = 16),
    axis.line = element_line(color = "black", size = 1), # Make axis lines bold and visible
    axis.ticks = element_line(color = "black", size = 1),  # Add bold tick marks
    axis.ticks.length = unit(0.25, "cm"),  # Increase tick mark length
    legend.position = "top",                  # Keep the legend at the top
    legend.text = element_text(size = 16),  # Increase legend title size
    panel.grid.major = element_blank(),  # Remove all grid lines
    panel.grid.minor = element_blank()   # Remove minor grid lines
  )+
  # Set x-axis breaks and ticks every 5 years
  scale_x_continuous(breaks = seq(min(graph2$year), max(graph2$year), by = 10)) +
  # Set y-axis limits and detailed breaks
  scale_y_continuous(
    limits = c(65, 85),  # Focus on the range of interest
  )
ggsave(
  filename = "figure_2.png", 
  width = 10, 
  height = 6, 
  dpi = 300, 
  bg = "white"
)


usa_me <- df %>%
  filter(ccode == 2 & year>1991)%>%  # Filter only rows where ccode == 2
  group_by(year) %>%
  summarize(usa_me = first(me), .groups = "drop")  # Ensure only one value per year

others_me <- df %>%
  filter(ccode != 2 & year>1991) %>%  # Exclude rows where ccode is 2 (USA)
  group_by(year) %>%
  summarize(me_others = sum(me, na.rm = TRUE), .groups = "drop")  # Calculate mean of `share`

russia_me <- df %>%
  filter(ccode == 365 & year>1991) %>%  # Exclude rows where ccode is 2 (USA)
  group_by(year) %>%
  summarize(russia_me = first(me), .groups = "drop")  # Ensure only one value per year

graph3 <- usa_me %>%
  inner_join(others_me, by = "year")

graph3 <- graph3 %>%
  inner_join(russia_me, by = "year")


graph3 <- graph3 %>%
  mutate(
    usa_me = usa_me / 1000000000,
    me_others = me_others / 1000000000,
    russia_me = russia_me / 1000000000
  )


ggplot(graph3, aes(x = year)) +
  # Plot line for US share
  geom_line(aes(y = usa_me, color = "US ME"), size = 1.2) +
  # Plot line for non-US NATO allies
  geom_line(aes(y = me_others, color = "Non-US NATO ME"), size = 1.2) +
  # Plot line for Russia's military expenditure
  geom_line(aes(y = russia_me, color = "Russian ME"), size = 1.2) +
  # Add solid vertical lines for significant years
  geom_vline(xintercept = c(2014, 2022), linetype = "dashed", color = "black", size = 0.8) +
  # Offset labels for vertical lines to avoid overlap
  annotate("text", x = 2014 + 1, y = 0.9 * max(c(graph1$usa_me, graph1$me_others, graph1$russia_me), na.rm = TRUE),
           label = "Crimean Invasion", angle = 90, hjust = -1.0, fontface = "bold", color = "black",size = 4.5) +
  annotate("text", x = 2022 + 1, y = 0.9 * max(c(graph1$usa_me, graph1$me_others, graph1$russia_me), na.rm = TRUE),
           label = "Ukraine War", angle = 90, hjust = -1.9, fontface = "bold", color = "black",size = 4.5) +
  # Labels and title
  labs(
    x = "Year",
    y = "ME (in billions)",
    color = "Group"
  ) +
  # Improved color scheme
  scale_color_manual(values = c("US ME" = "red", "Non-US NATO ME" = "blue", "Russian ME" = "orange"), name = NULL,
                     limits = c("US ME", "Non-US NATO ME", "Russian ME")) +
  # Customize the x-axis with tick marks every 5 years
  scale_x_continuous(
    breaks = seq(min(graph1$year, na.rm = TRUE), max(graph1$year, na.rm = TRUE), by = 5),
    minor_breaks = NULL
  ) +
  # Customize theme
  theme_minimal(base_size = 14) +
  theme(
    axis.text = element_text(color = "black",size = 16),
    axis.title = element_text(color = "black", size = 16),
    axis.line = element_line(color = "black", size = 1), # Make axis lines bold and visible
    axis.ticks = element_line(color = "black", size = 1),  # Add bold tick marks
    axis.ticks.length = unit(0.25, "cm"),  # Increase tick mark length
    legend.position = "top",
    legend.justification = "center",          # Center the legend
    legend.direction = "vertical",# Keep the legend at the top
    legend.text = element_text(size = 14),  # Increase legend title size
    panel.grid.major = element_blank(),  # Remove all grid lines
    panel.grid.minor = element_blank()   # Remove minor grid lines
  )
# Add white background to the plot
plot_with_white_bg <- last_plot() + 
  theme(
    plot.background = element_rect(fill = "white", color = NA),
    panel.background = element_rect(fill = "white", color = NA)
  )

# Save the plot
ggsave(
  filename = "figure_3.png", 
  plot = plot_with_white_bg, 
  width = 10, 
  height = 6, 
  dpi = 300, 
  units = "in"
)

